//
//  TitleViewController.m
//  iOS
//
//  Created by mokai on 14/11/19.
//  Copyright (c) 2014年 mokai. All rights reserved.
//

#import "NavBarViewController.h"

@interface NavBarViewController ()

@end

@implementation NavBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if ( ios_version >= 7.0)
    {
        self.edgesForExtendedLayout = UIRectEdgeNone;
        self.extendedLayoutIncludesOpaqueBars = NO;
        self.modalPresentationCapturesStatusBarAppearance = NO;
        self.navigationController.navigationBar.translucent = NO;
    
        self.statusView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screen_w, status_h)];
        self.statusView.backgroundColor = [UIColor themeColor];
        [self.view addSubview:self.statusView];
        self.view.backgroundColor = [UIColor themeBgColor];
    }
    
    //NavBar
    CGRect navBarRc = ios_version < 7.0 ?  CGRectMake(0, 0, screen_w, navbar_h) : CGRectMake(0, status_h, screen_w, navbar_h);
    self.navBarView = [[UIView alloc] initWithFrame:navBarRc];
    self.navBarView.backgroundColor = [UIColor themeColor];
    [self.view addSubview:self.navBarView];
    
    //title
    _titleLb = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, screen_w-120, navbar_h)];
    _titleLb.font = font(18);
    _titleLb.textColor = [UIColor whiteColor];
    _titleLb.backgroundColor = [UIColor clearColor];
    _titleLb.textAlignment = TEXT_ALIGNMENT_CENTER;
    [self.navBarView addSubview:_titleLb];
    
}

#pragma mark NavBar相关
/**
 *  显示
 */
-(void)showNavBarView{
    self.navBarView.hidden = NO;
}
/**
 *  隐藏
 */
-(void)hideNavBarView{
    self.navBarView.hidden = YES;
}

/**
 *  设置标题
 *
 */
-(void)setTitle:(NSString *)title{
    self.titleLb.text = title;
}

-(NSString *)title{
    return self.titleLb.text;
}
/**
 *  设置返回按钮
 *
 */
-(void)setBackItem:(NSString *)title{
    [self setBackItem:title imageName:nil];
}
/**
 *  设置返回按钮以及图片
 *
 */
-(void)setBackItem:(NSString *)title imageName:(NSString *)imageName{
    [self setLeftItem:title imageName:str_isblank(imageName)?theme_ic_back:imageName];
}
/**
 *  设置左边按钮
 *
 */
- (void)setLeftItem:(NSString *)title {
    [self setNavBarItemWithTitle:title imageName:nil image:nil left:YES];
}
-(void)setLeftItemWithImage:(UIImage *)image{
    [self setNavBarItemWithTitle:nil imageName:nil image:image left:YES];
}
-(void)setLeftItemWithImageName:(NSString *)imageName{
    [self setNavBarItemWithTitle:nil imageName:imageName image:nil left:YES];
}
- (void)setLeftItem:(NSString *)title imageName:(NSString *)imageName{
    [self setNavBarItemWithTitle:title imageName:imageName image:nil left:YES];
}

/**
 *  设置右边按钮
 *
 */
- (void)setRightItem:(NSString *)title{
    [self setNavBarItemWithTitle:title imageName:nil  image:nil left:NO];
}
-(void)setRightItemWithImageName:(NSString *)imageName{
    [self setNavBarItemWithTitle:nil imageName:imageName image:nil left:NO];
}
-(void)setRightItemWithImage:(UIImage *)image{
    [self setNavBarItemWithTitle:nil imageName:nil image:image left:NO];
}
- (void)setRightItem:(NSString *)title imageName:(NSString *)imageName{
    [self setNavBarItemWithTitle:title imageName:imageName image:nil left:NO];
}

- (void)setNavBarItemWithTitle:(NSString*)title imageName:(NSString*)imageName image:(UIImage *)image left:(BOOL)left
{
    CGFloat itemWidth = title && (imageName || image) ? navbar_item_w : navbar_h+view_space;
    UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, itemWidth , navbar_h)];
    button.titleLabel.font = font(16);
    button.backgroundColor = [UIColor clearColor];
    if (title) {
        [button setTitle:title forState:UIControlStateNormal];
        [button setTitleColor:[UIColor themeBtnColor] forState:UIControlStateNormal];
    }
    if (imageName || image) {
        UIImage* icImage = image ? image : [UIImage themeImageNamed:imageName];
        if (icImage) {
            [button setImage:icImage forState:UIControlStateNormal];
            if (left) {
                button.titleEdgeInsets = UIEdgeInsetsMake(0,  view_space, 0, 0);
            }else{
                button.imageEdgeInsets = UIEdgeInsetsMake(0,  0, 0, view_space);
            }
        }
    }
    if(left){
        button.titleLabel.textAlignment = NSTextAlignmentLeft;
        [button addTarget:self action:@selector(leftItemClick:) forControlEvents:UIControlEventTouchUpInside];
    }else{
        button.titleLabel.textAlignment = NSTextAlignmentRight;
        [button addTarget:self action:@selector(rightItemClick:) forControlEvents:UIControlEventTouchUpInside];
        button.left = screen_w-button.width;
    }
    [_navBarView addSubview:button];
}

- (void)leftItemClick:(UIButton*)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)rightItemClick:(UIButton*)sender
{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}

@end
